<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;


class Student extends Model
{
    use HasFactory;
    // 如果你的表名不是 'students'，可以指定
    protected $table = 'students';
    /**
     * 不可批量赋值的属性
     *
     * @var array
     */
    protected $guarded = [];
    public  static function list($query): \Illuminate\Database\Eloquent\Collection|array
    {
        $temp = self::query(); // 初始化查询构造器

        if(self::isValidKey($query,'key')){
            $temp->where( 'address', 'like', '%' . $query['key']. '%' );
        }

        if(self::isValidKey($query,'sex')){
            $temp->where('gender',$query['sex']);
        }
        if (self::isValidKey($query,'major')){
            $temp->where('major',$query['major']);
        }
        return  $temp->get();
    }
    private  static function isValidKey($query,$key) {
        return isset($query[$key]) && $query[$key] !== "";
    }

}
